package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.dto.request.PhoneDetailsReq;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.PhoneDetailsEntity;
import com.yishusmt.mobsmt.mapper.PhoneDetailsEntityMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.FacilityService;
import com.yishusmt.mobsmt.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 获取设备信息
 *
 * @author zhangyi
 * @date 2018/8/10 16:56
 */
@Service
@Slf4j
public class FacilityServiceImpl implements FacilityService {

    @Autowired
    private PhoneDetailsEntityMapper phoneDetailsEntityMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 存储设备信息
     *
     * @param pdReq
     */
    @Override
    public void getFacility(PhoneDetailsReq pdReq) {
        //手机设备信息实体
        PhoneDetailsEntity pd = new PhoneDetailsEntity();
        pd.setImei(pdReq.getImei());
        pd.setModel(pdReq.getModel());
        pd.setUuid(pdReq.getUuid());
        pd.setLanguage(pdReq.getLanguage());

        //用户id
        String phone = pdReq.getPhone() != null ? pdReq.getPhone():"";
        UserRes userRes = userInfoMapper.selectUserSelective(phone);
        if(userRes != null) {
            pd.setUserId(userRes.getId());
        }
        pd.setCreateTime(DateUtil.getCurrentDate());
        pd.setUpdateTime(DateUtil.getCurrentDate());
        int i = phoneDetailsEntityMapper.insertSelective(pd);
        if(0 == i){
            log.info("获取手机信息失败！");
        }else{
            log.info("获取手机信息成功！");
        }
    }
}
